﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using CuongHanh.Context;
using CuongHanh.DAL;
using CuongHanh.Models;
using CuongHanh.Models.Filter;

#endregion

namespace CuongHanh.Service
{
    public class ShippingCostService : BaseService
    {
        public static string Gets(int start,
                int limit,
                ShippingCostFilter filter)
        {
            Expression<Func<ServiceFee, bool>> clientWhere = null;
            if(filter != null)
            {
                clientWhere = c => true;
                if(filter.Id.HasValue && filter.Id > 0)
                {
                    Func<ServiceFee, bool> prefix = clientWhere.Compile();
                    clientWhere = c => prefix(c) && c.Id == filter.Id;
                }
                if(!string.IsNullOrWhiteSpace(filter.CountryCode))
                {
                    Func<ServiceFee, bool> prefix = clientWhere.Compile();
                    clientWhere = c => prefix(c) && c.Code == filter.CountryCode;
                }
                if(!string.IsNullOrWhiteSpace(filter.CountryName))
                {
                    Func<ServiceFee, bool> prefix = clientWhere.Compile();
                    clientWhere = c => prefix(c) && c.Name == filter.CountryName;
                }
            }
            IEnumerable<ServiceFee> query = CacheContext<ServiceFee>.Singleton.GetObjectsByFilter(clientWhere);
            return FormatResult(query.Count(),
                    query.OrderBy(c => c.Name).Skip(start).Take(limit));
        }

        public static ServiceFee Find(int id)
        {
            return CacheContext<ServiceFee>.Singleton.GetObject(id);
        }

        public static UpdateResult Insert(ServiceFee model)
        {
            using (UnitOfWork _unitOfWork = new UnitOfWork())
            {
                _unitOfWork.Insert(model);
                _unitOfWork.Save();
                CacheContext<ServiceFee>.Singleton.UpdateCached(model);
                return new UpdateResult(1,
                        string.Format("Insert ShippingCost {0} successful!",
                                model.Id));
            }
        }

        public static UpdateResult Update(ServiceFee newModel)
        {
            using (UnitOfWork _unitOfWork = new UnitOfWork())
            {
                ServiceFee model = _unitOfWork.GetById<ServiceFee>(newModel.Id);
                if(model == null)
                    return new UpdateResult(4,
                            string.Format("Update ShippingCost {0} error!",
                                    newModel.Id));
                model.UpdateInfo(newModel);
                _unitOfWork.Save();
                CacheContext<ServiceFee>.Singleton.UpdateCached(model);
                return new UpdateResult(1,
                        string.Format("Update ShippingCost {0} successful!",
                                newModel.Id));
            }
        }

        public static UpdateResult Delete(int id)
        {
            using (UnitOfWork _unitOfWork = new UnitOfWork())
            {
                ServiceFee oldModel = _unitOfWork.GetById<ServiceFee>(id);
                if(oldModel == null)
                    return new UpdateResult(4,
                            string.Format("Delete ShippingCost {0} error!",
                                    id));
                oldModel.Delete();
                _unitOfWork.Save();
                CacheContext<ServiceFee>.Singleton.UpdateCached(oldModel);
                return new UpdateResult(1,
                        string.Format("Delete ShippingCost {0} successful!",
                                id));
            }
        }

        public static List<Tuple<int, string, long>> GetCategoriesComboByBranch(int branchId)
        {
            List<Tuple<int, string, long>> models = CacheContext<ServiceFee>.Singleton.GetObjectsByFilter(c => !c.Inactive)
                    .Select(c => new Tuple<int, string, long>(
                            c.Id,
                            c.Name,
                            c.ShippingCost
                            )).ToList();

            return models;
        }
    }
}
